import { _decorator, Component, Node, v3, Vec3, Vec2 } from 'cc';
import { GlobalEnum } from '../../../Init/Config/GlobalEnum';
import GlobalPool from '../../../Init/Tools/GlobalPool';
import { MappingObj } from './MappingObj';
import { PeopleAnim } from './PeopleAnim';
const { ccclass, property } = _decorator;

@ccclass('MappingPeople')
export class MappingPeople extends MappingObj {
    public anim: PeopleAnim = null;
    public curState: GlobalEnum.EnemyState = null;
    public isRun = false;

    //------旋转
    public _rotVec = v3();
    //目标弧度
    public _toRadianY = 0;
    //初始弧度
    public _curRadianY = -Math.PI * 0.5;
    //旋转方向 
    public _rotDirec = 1;
    //旋转速度
    public _rotSpd = Math.PI * 4;

    //待机时朝向
    private idleRadianY = 0;

    constructor(node: Node, mp: { node: Node, state: number }) {
        super(node, mp);
    }

    public init() {
        this.idleRadianY = this.mappingProps.node.eulerAngles.z * 0.01745;
        this.updateProp();
    }

    public update(dt: any): void {
        this.updateProp();
        this.rotation(dt);
    }

    public updateProp() {
        //回收
        if (!this.mappingProps.node.active) {
            GlobalPool.put(this.node);
            return;
        }

        //位置
        this.tmpP.set(this.mappingProps.node.position);
        this.tmpP.z = -this.tmpP.y;
        this.tmpP.y = 0;
        this.node.setPosition(this.tmpP.multiplyScalar(this.s3d));

        //检查状态
        if (this.mappingProps.state !== null && (this.curState != this.mappingProps.state ||
            this.isRun != this.mappingProps.node[GlobalEnum.ExtralProp.PeopleRun])) {
            this.curState = this.mappingProps.state;
            this.isRun = this.mappingProps.node[GlobalEnum.ExtralProp.PeopleRun];

            let clip;
            if (this.curState == GlobalEnum.EnemyState.Alive) {
                clip = this.isRun ? GlobalEnum.PeopleClips.Run : GlobalEnum.PeopleClips.Idle;
            } else {
                clip = GlobalEnum.PeopleClips.Death;
            }
            this.playAnim(clip);
        }
        //旋转
        const rot: Vec2 = this.mappingProps.node[GlobalEnum.ExtralProp.curLineVec2d];
        let r = this.isRun ? Math.atan2(rot.y, rot.x) : this.idleRadianY;
        this.calRotation(r);

    }

    public playAnim(clip) {
        if (!this.anim) {
            this.anim = this.node.getComponent(PeopleAnim);
        }
        this.anim.playAnim(clip);
    }

    //旋转3d
    public rotation(dt) {
        if (this._curRadianY !== this._toRadianY) {
            let step = this._rotSpd * dt;
            if (step > Math.abs(this._curRadianY - this._toRadianY)) {
                this._curRadianY = this._toRadianY;
            } else {
                step *= this._rotDirec;
                this._curRadianY += step;
            }
            this._rotVec.set(0, this._curRadianY * 57.3 + 90, 0);
            this.node.eulerAngles = this._rotVec;
        }
    }

    //根据目标旋转角度计算出旋转方式-弧度计算
    public calRotation(r: number) {
        if (undefined == r) return;
        this._toRadianY = r;
        //1.0 将当前角色角度+目标角度转换到360之内-弧度计算
        const pi = Math.PI;
        if (this._toRadianY < 0) {
            this._toRadianY += pi * 2;
        }
        this._curRadianY = this._curRadianY % (pi * 2);
        if (this._curRadianY > pi * 2) {
            this._toRadianY -= pi * 2;
        } else if (this._curRadianY < 0) {
            this._toRadianY += pi * 2;
        }
        //2.0 判断差值范围
        let sub = this._toRadianY - this._curRadianY;
        if (sub > pi) {
            //反向
            this._curRadianY += pi * 2;
            this._rotDirec = -1;
        } else if (sub < -pi) {
            //反向
            this._toRadianY += pi * 2;
            this._rotDirec = 1;
        } else if (sub > 0) {
            this._rotDirec = 1;
        } else if (sub < 0) {
            this._rotDirec = -1;
        }
    }
}

